package com.caopeng.javaee.controller;

import com.caopeng.javaee.dao.CourseJpa;
import com.caopeng.javaee.dao.StudentJpa;
import com.caopeng.javaee.dao.TeacherJpa;
import com.caopeng.javaee.entity.course;
import com.caopeng.javaee.entity.student;
import com.caopeng.javaee.entity.teacher;
import com.caopeng.javaee.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: javaee
 * @description: 只用来处理登陆这一块的逻辑
 * @author: caopeng
 * @create: 2018-12-06 10:32
 **/
@Controller
public class LoginAndfoundController {
    @Autowired
    LoginService loginService;
    @Autowired
    StudentJpa studentjpa;
    @Autowired
    TeacherJpa teacherjpa;
    @Autowired
    CourseJpa coursejpa;
    @RequestMapping(value = "login")
    public  String login(){
        return "login";
    }
    @RequestMapping(value = "check_login",method = RequestMethod.POST)
    public String check_login(HttpServletRequest request, Model model, HttpSession session){
        String account=request.getParameter("account");
        String password=request.getParameter("password");
        String user_type=request.getParameter("user");
        if(user_type.equals("student")) {
            student check_student=loginService.SfindByAccountAndPassword(account,password);
            if(check_student==null)
            {
                return "login";
            }
            else
            {
                model.addAttribute("user_name",check_student.getName());
                session.setAttribute("user_id",check_student.getId());
                System.out.println(session.getAttribute("user_id"));
                return "student_index";
            }
        }
        else if(user_type.equals("teacher")) {
            List<teacher> teachers=loginService.TfindByAccountAndPassword(account,password);
            if(teachers.size()>0)
            {
                model.addAttribute("user_name",teachers.get(0).getName());
                session.setAttribute("user_id",teachers.get(0).getId());
                List<course> allcourse=coursejpa.findByCourseTeacherId(String.valueOf(session.getAttribute("user_id")));
                System.out.println("打印session值");
                System.out.println(session.getAttribute("user_id"));
                System.out.println("打印课程长度"+allcourse.size());
                model.addAttribute("teacher_class",allcourse);
                return "teacher_index";
            }
            else
            {
                return "login";
            }
        }
        return "error(test)";
    }
    @RequestMapping(value = "to_forgetPassword")
    public String to_forgetPassword(){
        return "forget_password";
    }
    @RequestMapping(value = "/forget_password",method = RequestMethod.POST)
    public @ResponseBody String forget_password(String account,String phone,String code,String user,String password,HttpServletRequest request){
        System.out.println(account+" "+phone+" "+code+" "+user+" "+password);
        if(user.equals("学生"))
        {
            student find_student=loginService.SfindByAccountAndPhone(account,phone);
            if(find_student==null){
                return "false";
            }
            else{
                find_student.setPassword(password);
                studentjpa.save(find_student);
                return "true";
            }
        }
        else
        {
            teacher teacher_find=loginService.TfindByAccountAndPhone(account,phone);
            if(teacher_find==null){
                return "false";
            }
            else{
                teacher_find.setPassword(password);
                teacherjpa.save(teacher_find);
                return "true";
            }
        }
    }

}

